﻿using Furion;
using Furion.UnifyResult;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Photo.Web.Core.Setup;
using Photo.Web.Core.Filters;

namespace Photo.Web.Core;

public class Startup : AppStartup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddJwt<JwtHandler>(enableGlobalAuthorize: true);//开启全局接口授权
        
        services.AddVirtualFileServer();//注册虚拟文件系统服务(读取资源文件)

        services.AddCorsAccessor();

        services.AddControllersWithViews()
                .AddNewtonsoftJson(options =>
                {
                    options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                });

        services.AddControllers()
                .AddInjectWithUnifyResult<RESTfulResultProvider>();

        //日志配置
        services.AddLogSetup();

        //去除入参string前后空格
        services.Configure<MvcOptions>(options =>
        {
            options.Filters.Add<TrimInputActionFilter>();
        });

        //sqlsugar数据库配置
        services.AddSqlsugarSetup(App.Configuration["ConnectionStrings:BaseNetDB"]);
        
        //添加事件总线
        services.AddEventBus();



        ////配置redis缓存
        //RedisHelper.Initialization(new CSRedisClient(App.Configuration["AppInfo:RedisConConfig"]));

    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseCorsAccessor();

        app.UseAuthentication();
        app.UseAuthorization();

        app.UseInject(string.Empty);

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });

    }
}
